﻿using System;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Practices.Unity;
using XamlaApp.ViewModels;
using XamlaApp.Views.ElementMenuButtonViews;
using XamlaApp.Views.ElementPropertyViews;
using System.Windows.Media;
using XamlaApp.Interfaces;

namespace XamlaApp.Views.ElementViews
{
    public partial class TextBlockView : UserControl, IXamlaElement
    {
        [Dependency]
        public TextBlockViewModel VM { get; set; }

        [Dependency]
        public TextBlockProperties PropView { get; set; }

        public TextBlockView()
        {
            InitializeComponent();
            Loaded += TextBlockView_Loaded;
        }

        void TextBlockView_Loaded(object sender, RoutedEventArgs e)
        {
            VM.RegisterPositionBoundElement(this);
            VM.RegisterProjectionBoundElement(this);
            PropView.VM = VM; //bind the property view model to this view model
            DataContext = VM;

        }

        #region IXamlaElement Members

        public UserControl Propertyview
        {
            get { return PropView; }
        }

        public PlaneProjection ElementProjection
        {
            get { return projTest; }
        }

        public string ControlName { get; set; }

        public FrameworkElementViewModel ViewModel
        {
            get { return VM; }
        }

        public UserControl MenuButton
        {
            get
            {
                return Container.AppContainer.Resolve<TextBlockMenuButton>();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion

        private void tbXamlaTextBlockEdit_TextChanged(object sender, TextChangedEventArgs e)
        {
            VM.Text = tbXamlaTextBlockEdit.Text;
        }

        private void tbXamlaTextBlockEdit_LostFocus(object sender, RoutedEventArgs e)
        {
            var uivm = Container.AppContainer.Resolve<MainUIViewModel>();

            //if we lost focus because of a tab event, then the current element
            //will still be this element, so put it into a selected state
            //otherwise put it into a normal state
            if (this.Equals(uivm.CurrentElement))
                VisualStateManager.GoToState(this, "Selected", false);
            else
                VisualStateManager.GoToState(this, "Normal", false);

            VM.EditMode = false;
            uivm.UpdateControlEditor();

        }

        private void tbXamlaTextBlockEdit_GotFocus(object sender, RoutedEventArgs e)
        {
            VM.EditMode = true;
            if (!VM.IsActiveElement)
            {
                var uivm = Container.AppContainer.Resolve<MainUIViewModel>();
                uivm.CurrentElement = this;
                uivm.PropertyView = PropView;
            }
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            VM.EditMode = true;
            var uivm = Container.AppContainer.Resolve<MainUIViewModel>();
            uivm.UpdateControlEditor();
            VisualStateManager.GoToState(this, "EditMode", false);
            tbXamlaTextBlockEdit.Focus();
        }
    }
}
